﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Agr.CQRS.Domain;
using Agr.Questionnaire.Core.Domain.Qnnaire.Events;

namespace Agr.Questionnaire.Core.Domain.Qnnaire
{
    public class QnnaireSection : Entity
    {
        public const string DEFAULT_SECTION = "DefaultSection";

        internal string Name { get; set; }
        internal IList<AnswerableQuestion> AnswerableQuestions { get; set; }

        public QnnaireSection()
        {
            AnswerableQuestions = new List<AnswerableQuestion>();
        }

        public void AddQuestion(AnswerableQuestion aq)
        {
            AnswerableQuestions.Add(aq);
            Aggregate.RegisterEntity(aq);
        }

        public AnswerableQuestion GetQuestion(int sequenceNumber)
        {
            return (from q in AnswerableQuestions where q.SequenceNumber == sequenceNumber select q).FirstOrDefault();
        }

        public void AddNewTextQuestion(string questionText)
        {
            HandleNewEvent(new TextQuestionAddedEvent(questionText, AnswerableQuestions.Count));
        }
        private void handle(TextQuestionAddedEvent ev)
        {
            var q = new AnswerableQuestion
            {
                Question = new TextQuestion { Text = ev.QuestionText },
                SequenceNumber = ev.SequenceNumber
            };
            AddQuestion(q);
        }
    }
}
